import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import UnoCSS from 'unocss/vite';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import { presetUno } from '@unocss/preset-uno';
import path from 'path';

const host = process.env.TAURI_DEV_HOST;

// https://vitejs.dev/config/
export default defineConfig(async () => ({
  // base: '/tauri-start', // 打包找不到资源
  plugins: [
    vue(),
    UnoCSS({
      presets: [presetUno()]
    }),
    AutoImport({
      // 配置NaiveUi组件自动导入
      resolvers: [
        NaiveUiResolver()
      ],
      imports: ['vue', 'vue-router', '@vueuse/core'], // 自动导入的依赖库数组
      dirs: ['./src/constants/**', './src/hooks/**'], // 指定要导出的目录
      dts: './auto-imports.d.ts' // 自动导入类型定义文件路径
    }),
    Components({
      // 配置NaiveUi组件自动导入
      resolvers: [
        NaiveUiResolver(),
        // 自定义解析器
        // (name: string) => {
        //   if (name.startsWith('BaseForm')) {
        //     return {name: 'default', from: '@/components/base/form/index.ts'};
        //   }
        // }
      ],
      // 指定要扫描的组件目录
      dirs: ['src/components'], // 指定要扫描的目录
      exclude: ['src/components/**/register.ts'], // 排除目录
      extensions: ['vue', 'ts'], // 组件文件扩展名
      dts: './components.d.ts', // 生成类型声明文件
      directoryAsNamespace: true // 根据目录路径生成组件名称
    }),
    createSvgIconsPlugin({
      iconDirs: [path.resolve(__dirname, './src/assets/icon')],
      symbolId: 'fx-icon-[dir]-[name]'
    })
  ],
  // css配置
  css: {
    preprocessorOptions: {
      scss: {
        // 修改api调用方式；处理兼容性
        api: 'modern-compiler'
      }
    }
  },
  // 打包配置
  build: {
    rollupOptions: {
      output: {
        // 用于从入口点创建的块的打包输出格式[name]表示文件名,[hash]表示该文件内容hash值
        entryFileNames: 'static/js/[name]-[hash].js',
        // 用于命名代码拆分时创建的共享块的输出命名
        chunkFileNames: 'static/js/[name]-[hash].js',
        // 用于输出静态资源的命名，[ext]表示文件扩展名
        assetFileNames: 'static/assets/[ext]/[name]-[hash].[ext]',
        // 最小化拆分包
        manualChunks: (id) => {
          if (id.includes('node_modules')) {
            return id.toString().split('node_modules/')[1].split('/')[0].toString();
          }
        }
      }
    }
  },
  // 配置别名
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src')
    }
  },
  // Vite options tailored for Tauri development and only applied in `tauri dev` or `tauri build`
  //
  // 1. prevent vite from obscuring rust errors
  clearScreen: false,
  // 2. tauri expects a fixed port, fail if that port is not available
  server: {
    port: 1420,
    strictPort: true,
    host: host || false,
    hmr: host
      ? {
        protocol: 'ws',
        host,
        port: 1421
      }
      : undefined,
    watch: {
      // 3. tell vite to ignore watching `src-tauri`
      ignored: ['**/src-tauri/**']
    }
  }
}));
